package com.sino.business.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.opensymphony.xwork2.ActionContext;
import com.sino.bean.Basicinfo;
import com.sino.bean.Qxkw;
import com.sino.bean.Tjyj;
import com.sino.bean.Users;
import com.sino.business.IPatentManageService;

import com.sino.dao.IPatentManageDAO;
import com.sino.model.BasicInfoModel;
import com.sino.model.ExpertModel;
import com.sino.model.StaticModel;
import com.sino.model.TjyjModel;
import com.sino.second.bean.TblXmfs1;
import com.sino.util.DateUtils;

public class PatentManageServiceImp implements IPatentManageService {

	/**
	 * 用户查询
	 */
	public List<LoginInfo> getUser(String qxkw, String hql) {

		String strhql = "";

		if (hql != null && hql.length() > 0) {
			if (qxkw != null && !qxkw.equals("0000")) {
				strhql = "from Users users where users.qxkw=" + qxkw + " and "
						+ hql;
			} else {
				strhql = "from Users users  where " + hql;
			}
		} else {
			if (qxkw != null && !qxkw.equals("0000")) {
				strhql = "from Users users where users.qxkw=" + qxkw;
			} else {
				strhql = "from Users users";
			}
		}

		return this.poListToVoList(this.getIpatentManageDAO().findUser(qxkw,
				strhql));
	}

	private IPatentManageDAO ipatentManageDAO;

	public List<BasicInfoModel> getBasicSituation() {
		// TODO Auto-generated method stub
		return null;
	}

	public IPatentManageDAO getIpatentManageDAO() {
		return ipatentManageDAO;
	}

	public void setIpatentManageDAO(IPatentManageDAO ipatentManageDAO) {
		this.ipatentManageDAO = ipatentManageDAO;
	}

	public List<LoginInfo> getUser(String qxkw) {

		List<Users> userList = this.getIpatentManageDAO().findUser(qxkw);
		return poListToVoList(userList);
	}

	public List<LoginInfo> poListToVoList(List<Users> nList) {
		List<LoginInfo> tempList = new ArrayList<LoginInfo>();
		Iterator<Users> it = nList.iterator();
		while (it.hasNext()) {
			LoginInfo nm = this.getLoginInfo(it.next());
			tempList.add(nm);

		}
		return tempList;
	}

	public List<BasicInfoModel> poListToVoList2(List<Basicinfo> nList) {
		List<BasicInfoModel> tempList = new ArrayList<BasicInfoModel>();
		Iterator<Basicinfo> it = nList.iterator();
		while (it.hasNext()) {
			BasicInfoModel nm = this.getBasicInfoModel(it.next());
			tempList.add(nm);

		}
		return tempList;
	}

	public LoginInfo getUsersByName(String name, String password) {
		boolean flag = this.getIpatentManageDAO().findUsersByName(name,
				password);
		LoginInfo logininfo = null;

		if (flag == true) {
			Users user = this.getIpatentManageDAO().findUsersByValidate(name,
					password);

			logininfo = this.getLoginInfo(user);
		}
		return logininfo;
	}

	public boolean verifyPassword(String name, String password) {
		boolean flag = this.getIpatentManageDAO().findUsersByName(name,
				password);
		// System.out.println(flag);
		return flag;
	}

	public void saveUser(LoginInfo logininfo) {
		Users user = this.getUsers(logininfo);
		this.getIpatentManageDAO().saveUser(user);

	}

	public LoginInfo getLoginInfo(Users user) {
		LoginInfo logininfo = new LoginInfo();
		logininfo.setId(user.getId());
		logininfo.setName(user.getName());
		logininfo.setPassword(user.getPassword());
		logininfo.setQxkw(user.getQxkw().getId());
		logininfo.setQxkwmc(user.getQxkw().getName());

		logininfo.setTruename(user.getTruename());
		logininfo.setLxdh(user.getLxdh());

		logininfo.setEmail(user.getEmail());
		logininfo.setYddh(user.getYddh());

		return logininfo;
	}

	public Users getUsers(LoginInfo logininfo) {
		Users user = new Users();
		user.setId(logininfo.getId());
		user.setName(logininfo.getName());
		user.setPassword(logininfo.getPassword());
		Qxkw qxkw = new Qxkw();
		qxkw.setId(logininfo.getQxkw());
		user.setQxkw(qxkw);
		user.setTruename(logininfo.getTruename());
		user.setLxdh(logininfo.getLxdh());
		user.setEmail(logininfo.getEmail());
		user.setYddh(logininfo.getYddh());

		return user;
	}

	public void deleteQxkw(String id) {
		this.ipatentManageDAO.deleteQxkw(id);

	}

	public void deleteUser(String id) {

		this.ipatentManageDAO.deleteUser(id, "");

	}

	public List<Qxkw> getQxkw() {
		List<Qxkw> qxkwList = this.getIpatentManageDAO().findQxkw();
		return qxkwList;
	}

	public void saveQxkw(Qxkw qxkw) {
		this.getIpatentManageDAO().saveQxkw(qxkw);

	}

	public boolean verifyUser(String name) {
		boolean flag = this.getIpatentManageDAO().findUsersByName(name);
		return flag;
	}

	public void updatePassword(LoginInfo loginInfo, Integer id) {
		Users user = this.getIpatentManageDAO().findUsersById(id);

		user.setPassword(loginInfo.getPassword());

		this.getIpatentManageDAO().updateUser(user);

	}

	public List<BasicInfoModel> getBasicInfoPageList(int firstRow, int maxRow,
			String hqlwhere, String qxkw) {
		String hql = "from Basicinfo as basicinfo  ";
		if (null != qxkw && qxkw.length() > 0) {
			if (qxkw.equals("0000") && hqlwhere.length() <= 0) {
				hql = hql;

			} else {

				hql = hql + " where  ";

			}

			if (!qxkw.equals("0000") && hqlwhere.length() > 0) {

				hql = hql + hqlwhere + " and basicinfo.qxkw=" + qxkw;

			} else if (!qxkw.equals("0000")) {

				hql = hql + " basicinfo.qxkw=" + qxkw;

			} else if (qxkw.equals("0000") && hqlwhere.length() > 0) {

				hql = hql + hqlwhere;

			}

		}

		hql = hql + " order by basicinfo.sqrq";

		// hql = hql + " order by basicinfo.sqrq";

		return this.poListToVoList2(getIpatentManageDAO()
				.findBasicinfoPageList(firstRow, maxRow, hql));
	}

	public int getBasicInfoPageCount(String hqlwhere, String qxkw) {
		String hql = "select count(*) from Basicinfo as basicinfo";

		if (null != qxkw && qxkw.length() > 0) {
			if (qxkw.equals("0000") && hqlwhere.length() <= 0) {
				hql = hql;

			} else {

				hql = hql + "  where ";

			}
			if (!qxkw.equals("0000") && hqlwhere.length() > 0) {

				hql = hql + hqlwhere + " and basicinfo.qxkw=" + qxkw;

			} else if (!qxkw.equals("0000")) {

				hql = hql + " basicinfo.qxkw=" + qxkw;

			} else if (qxkw.equals("0000") && hqlwhere.length() > 0) {

				hql = hql + hqlwhere;

			}
		}

		hql = hql + " order by basicinfo.zlrq desc";
		return this.getIpatentManageDAO().findNoticePageCount(hql);
	}

	public BasicInfoModel getBasicInfoModel(Basicinfo basicinfo) {
		ExpertModel em = (ExpertModel) ActionContext.getContext().getSession()
				.get("expertModel");
		BasicInfoModel basicInfoModel = new BasicInfoModel();
		basicInfoModel.setHy(basicinfo.getHy().getId());
		basicInfoModel.setLxdh(basicinfo.getLxdh());
		basicInfoModel.setFmr(basicinfo.getFmr());

		basicInfoModel.setLxr(basicinfo.getLxr());
		basicInfoModel.setName(basicinfo.getName());
		basicInfoModel.setPassw(basicinfo.getPassw());
		basicInfoModel.setQxkwmc(basicinfo.getQxkw().getName());
		basicInfoModel.setShbz(basicinfo.getShbz());
		basicInfoModel.setShrq(basicinfo.getShrq());
		basicInfoModel.setSqrq(basicinfo.getSqrq());
		basicInfoModel.setTxdz(basicinfo.getTxdz());
		basicInfoModel.setXk(basicinfo.getXk().getId());
		basicInfoModel.setYddh(basicinfo.getYddh());
		basicInfoModel.setYzbm(basicinfo.getYzbm());
		basicInfoModel.setZlid(basicinfo.getZlid());
		basicInfoModel.setZlid2(basicinfo.getZlid2());
		basicInfoModel.setZlqr(basicinfo.getZlqr());
		basicInfoModel.setZlrq(basicinfo.getZlrq());
		basicInfoModel.setZxbz(basicinfo.getZxbz());
		basicInfoModel.setSbr(basicinfo.getSbr());
		basicInfoModel.setXkName(basicinfo.getXk().getName());
		basicInfoModel.setZlid2Link(this.getZlidLink(basicinfo.getZlid2()));
		basicInfoModel.setEmail(basicinfo.getEmail());
		if (null != basicinfo.getOtherXkName()) {
			basicInfoModel.setOtherXkName(basicinfo.getOtherXkName());
		}
		Object o[] = null;

		if (em != null) {
			if (basicinfo.getXmfs1Set() != null
					&& basicinfo.getXmfs1Set().size() >= 1) {
				o = basicinfo.getXmfs1Set().toArray();

				for (Object o1 : o) {
					if (((TblXmfs1) o1).getTblZhuanjiarukuxinxibiao().getFBh()
							.equals(em.getFBh())) {
						basicInfoModel.setCheckReview1(true);
					}
					if (((TblXmfs1) o1).getTblZhuanjiarukuxinxibiao().getFBh()
							.equals(em.getFBh())
							&& basicinfo.getTxj1Set() != null
							&& basicinfo.getTxj1Set().size() >= 1) {
						basicInfoModel.setCheckResults1(true);

					}
				}

			}

		}
		return basicInfoModel;
	}

	// 专利链接外网处理
	public String getZlidLink(String zlid) {
		String outZlidStr = zlid;
		if ("zl".equalsIgnoreCase(zlid.substring(0, 2))) {
			outZlidStr = zlid.substring(2);
		}
		return outZlidStr.replaceAll(" ", "");

	}

	public void updateUser(LoginInfo logininfo, Integer id) {
		Users user = this.getIpatentManageDAO().findUsersById(id);
		user.setName(logininfo.getName());
		Qxkw qxkw = new Qxkw();
		qxkw.setId(logininfo.getQxkw());
		user.setQxkw(qxkw);
		user.setTruename(logininfo.getTruename());
		user.setLxdh(logininfo.getLxdh());
		user.setEmail(logininfo.getEmail());
		user.setYddh(logininfo.getYddh());
		this.getIpatentManageDAO().updateUser(user);

	}

	public Tjyj getTjyi(TjyjModel tjyjModel) {

		Tjyj tjyj = new Tjyj();
		tjyj.setId(tjyjModel.getId());
		tjyj.setCz(tjyjModel.getCz());
		tjyj.setCz1(tjyjModel.getCz1());
		tjyj.setLxdh(tjyjModel.getLxdh());
		tjyj.setLxdh1(tjyjModel.getLxdh1());
		// tjyj.setNyr(tjyjModel.getNyr());
		// tjyj.setNyr1(tjyjModel.getNyr1());
		tjyj.setTjdwm(tjyjModel.getTjdwm());
		tjyj.setTjfzr(tjyjModel.getTjfzr());
		tjyj.setTjyj(tjyjModel.getTjyj());

		tjyj.setYzm(tjyjModel.getYzm());
		tjyj.setYzm1(tjyjModel.getYzm1());
		tjyj.setTxdz1(tjyjModel.getTxdz1());
		tjyj.setEmail(tjyjModel.getEmail());
		tjyj.setEmail1(tjyjModel.getEmail1());
		tjyj.setZlid(tjyjModel.getZlid());

		if (tjyjModel.getMnyr() != null) {
			tjyj.setNyr(DateUtils.toDateTime(tjyjModel.getMnyr()));
		}
		if (tjyjModel.getMNyr1() != null) {
			tjyj.setNyr1(DateUtils.toDateTime(tjyjModel.getMNyr1()));
		}
		if (tjyjModel.getMtjnyr() != null) {
			tjyj.setTjnyr(DateUtils.toDateTime(tjyjModel.getMtjnyr()));
		}
		tjyj.setTxdz(tjyjModel.getTxdz());

		tjyj.setYzdw(tjyjModel.getYzdw());
		tjyj.setYzdw1(tjyjModel.getYzdw1());

		return tjyj;
	}

	public TjyjModel getTjyjRptByZlid(String zlid) {

		return this.getTjyiRpt(this.getIpatentManageDAO().findTjyjByZlid(zlid));

	}

	public TjyjModel getTjyjByZlid(String zlid) {
		Tjyj tjyj = this.getIpatentManageDAO().findTjyjByZlid(zlid);

		return this.getTjyi(tjyj);

	}

	public TjyjModel getTjyi(Tjyj tjyjModel) {
		TjyjModel tjyj = new TjyjModel();
		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");

		if (tjyjModel != null) {

			tjyj.setCz(tjyjModel.getCz());
			tjyj.setCz1(tjyjModel.getCz1());
			tjyj.setLxdh(tjyjModel.getLxdh());
			tjyj.setLxdh1(tjyjModel.getLxdh1());
			tjyj.setNyr(tjyjModel.getNyr());
			tjyj.setNyr1(tjyjModel.getNyr1());
			tjyj.setTjdwm(tjyjModel.getTjdwm());
			tjyj.setTjfzr(tjyjModel.getTjfzr());
			tjyj.setTjyj(tjyjModel.getTjyj());
			tjyj.setTxdz(tjyjModel.getTxdz());
			tjyj.setYzm(tjyjModel.getYzm());
			tjyj.setYzm1(tjyjModel.getYzm1());
			tjyj.setTxdz1(tjyjModel.getTxdz1());
			tjyj.setEmail(tjyjModel.getEmail());
			tjyj.setEmail1(tjyjModel.getEmail1());
			tjyj.setZlid(tjyjModel.getZlid());
			tjyj.setTxdz1(tjyjModel.getTxdz1());
			tjyj.setTxdz(tjyj.getTxdz());
			tjyj.setLxdh1(tjyj.getLxdh1());
			tjyj.setTjnyr(tjyjModel.getTjnyr());
			tjyj.setYzdw(tjyjModel.getYzdw());
			tjyj.setYzdw1(tjyjModel.getYzdw1());
			if (tjyjModel.getNyr() != null) {
				tjyj.setMnyr(format1.format(tjyj.getNyr()));
			}
			if (tjyjModel.getNyr1() != null) {
				tjyj.setMNyr1(format1.format(tjyj.getNyr1()));
			}
			if (tjyjModel.getTjnyr() != null) {
				tjyj.setMtjnyr(format1.format(tjyj.getTjnyr()));
			}
		} else {
			String aa = "";

		}

		return tjyj;
	}

	public TjyjModel getTjyiRpt(Tjyj tjyj) {
		TjyjModel tjyjModel = new TjyjModel();
		DateFormat format1 = new SimpleDateFormat("yyyy年MM月dd日");
		try {
			if (tjyj == null) {
				tjyj = new Tjyj();
			}
			BeanUtils.copyProperties(tjyjModel, tjyj);
			if (tjyj.getNyr() != null) {
				tjyjModel.setMnyr(format1.format(tjyj.getNyr()));
			}
			if (tjyj.getNyr1() != null) {
				tjyjModel.setMNyr1(format1.format(tjyj.getNyr1()));
			}
			if (tjyj.getTjnyr() != null) {
				tjyjModel.setMtjnyr(format1.format(tjyj.getTjnyr()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return tjyjModel;
	}

	public void saveTjyj(TjyjModel tjyjModel) {

		Tjyj ttjyj = this.ipatentManageDAO.findTjyjByZlid(tjyjModel.getZlid());
		Tjyj tjyj = null;

		if (ttjyj != null) {

			ttjyj.setCz(tjyjModel.getCz());
			ttjyj.setCz1(tjyjModel.getCz1());
			ttjyj.setLxdh(tjyjModel.getLxdh());
			ttjyj.setLxdh1(tjyjModel.getLxdh1());
			// tjyj.setNyr(tjyjModel.getNyr());
			// tjyj.setNyr1(tjyjModel.getNyr1());
			ttjyj.setTjdwm(tjyjModel.getTjdwm());
			ttjyj.setTjfzr(tjyjModel.getTjfzr());
			ttjyj.setTjyj(tjyjModel.getTjyj());

			ttjyj.setYzm(tjyjModel.getYzm());
			ttjyj.setYzm1(tjyjModel.getYzm1());
			ttjyj.setTxdz1(tjyjModel.getTxdz1());
			ttjyj.setEmail(tjyjModel.getEmail());
			ttjyj.setEmail1(tjyjModel.getEmail1());
			ttjyj.setZlid(tjyjModel.getZlid());

			if (tjyjModel.getMnyr() != null) {
				ttjyj.setNyr(DateUtils.toDateTime(tjyjModel.getMnyr()));
			}
			if (tjyjModel.getMNyr1() != null) {
				ttjyj.setNyr1(DateUtils.toDateTime(tjyjModel.getMNyr1()));
			}
			if (tjyjModel.getMtjnyr() != null) {
				ttjyj.setTjnyr(DateUtils.toDateTime(tjyjModel.getMtjnyr()));
			}
			ttjyj.setTxdz(tjyjModel.getTxdz());

			ttjyj.setYzdw(tjyjModel.getYzdw());
			ttjyj.setYzdw1(tjyjModel.getYzdw1());
			this.ipatentManageDAO.updateTjyj(ttjyj);
		} else {

			tjyj = this.getTjyi(tjyjModel);
			this.ipatentManageDAO.saveTjyj(tjyj);
		}

	}

	public String getQxkwLink(List qxkwList) {
		Iterator it = qxkwList.iterator();
		String qxkwStr = "";
		StaticModel qxkw = null;
		String tempStr = "";
		int i = 0;
		while (it.hasNext()) {
			qxkw = (StaticModel) it.next();
			i++;
			tempStr = "&nbsp;<a href='basicInfoQuery.action?bm_basicinfo$qxkw="
					+ qxkw.getId() + "' >" + qxkw.getName() + " </a> ";
			if (i == 15) {
				qxkwStr += "<br>";
			}
			qxkwStr += "| &nbsp;" + tempStr;

		}
		return qxkwStr;
	}

	public LoginInfo getLoginInfo(String id) {

		return this.getLoginInfo(this.ipatentManageDAO
				.findUsersById(new Integer(id)));
	}

	public void updateUsers(LoginInfo loginInfo) {

		Users user = this.getUsers(loginInfo);
		this.ipatentManageDAO.updateUser(user);

	}

	public void updateBasicInfoShbz(String flag, String[] shbz) {
		String shbzf = "";
		Basicinfo basicinfo;
		for (int i = 0; i < shbz.length; i++) {

			basicinfo = this.getIpatentManageDAO().findBasicInfoByZlid(shbz[i]);
			if (flag.equals("允许修改")) {

				shbzf = "0";
				this.updateBasicInfoShbzDo(basicinfo, shbzf);
			} else {

				shbzf = "1";
				this.updateBasicInfoShbzDo(basicinfo, shbzf);
			}

		}
	}

	public void updateBasicInfoShbzDo(Basicinfo basicinfo, String shbz) {

		basicinfo.setShbz(shbz);
		this.getIpatentManageDAO().updateBasicInfoShbz(basicinfo);

	}

	public Qxkw getQxkwById(String id) {
		Qxkw qxkw = null;
		if (null != id) {
			qxkw = this.ipatentManageDAO.findQxkwById(id);

		}
		return qxkw;
	}

}